/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.IllegalOrphanException;
import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.TipoAtributo;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Atributo;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author castlerock
 */
public class TipoAtributoJpaController {

    public TipoAtributoJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TipoAtributo tipoAtributo) {
        if (tipoAtributo.getAtributoList() == null) {
            tipoAtributo.setAtributoList(new ArrayList<Atributo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Atributo> attachedAtributoList = new ArrayList<Atributo>();
            for (Atributo atributoListAtributoToAttach : tipoAtributo.getAtributoList()) {
                atributoListAtributoToAttach = em.getReference(atributoListAtributoToAttach.getClass(), atributoListAtributoToAttach.getIdAtributo());
                attachedAtributoList.add(atributoListAtributoToAttach);
            }
            tipoAtributo.setAtributoList(attachedAtributoList);
            em.persist(tipoAtributo);
            for (Atributo atributoListAtributo : tipoAtributo.getAtributoList()) {
                TipoAtributo oldTipoAtributoOfAtributoListAtributo = atributoListAtributo.getTipoAtributo();
                atributoListAtributo.setTipoAtributo(tipoAtributo);
                atributoListAtributo = em.merge(atributoListAtributo);
                if (oldTipoAtributoOfAtributoListAtributo != null) {
                    oldTipoAtributoOfAtributoListAtributo.getAtributoList().remove(atributoListAtributo);
                    oldTipoAtributoOfAtributoListAtributo = em.merge(oldTipoAtributoOfAtributoListAtributo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TipoAtributo tipoAtributo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoAtributo persistentTipoAtributo = em.find(TipoAtributo.class, tipoAtributo.getIdTipoAtributo());
            List<Atributo> atributoListOld = persistentTipoAtributo.getAtributoList();
            List<Atributo> atributoListNew = tipoAtributo.getAtributoList();
            List<String> illegalOrphanMessages = null;
            for (Atributo atributoListOldAtributo : atributoListOld) {
                if (!atributoListNew.contains(atributoListOldAtributo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Atributo " + atributoListOldAtributo + " since its tipoAtributo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Atributo> attachedAtributoListNew = new ArrayList<Atributo>();
            for (Atributo atributoListNewAtributoToAttach : atributoListNew) {
                atributoListNewAtributoToAttach = em.getReference(atributoListNewAtributoToAttach.getClass(), atributoListNewAtributoToAttach.getIdAtributo());
                attachedAtributoListNew.add(atributoListNewAtributoToAttach);
            }
            atributoListNew = attachedAtributoListNew;
            tipoAtributo.setAtributoList(atributoListNew);
            tipoAtributo = em.merge(tipoAtributo);
            for (Atributo atributoListNewAtributo : atributoListNew) {
                if (!atributoListOld.contains(atributoListNewAtributo)) {
                    TipoAtributo oldTipoAtributoOfAtributoListNewAtributo = atributoListNewAtributo.getTipoAtributo();
                    atributoListNewAtributo.setTipoAtributo(tipoAtributo);
                    atributoListNewAtributo = em.merge(atributoListNewAtributo);
                    if (oldTipoAtributoOfAtributoListNewAtributo != null && !oldTipoAtributoOfAtributoListNewAtributo.equals(tipoAtributo)) {
                        oldTipoAtributoOfAtributoListNewAtributo.getAtributoList().remove(atributoListNewAtributo);
                        oldTipoAtributoOfAtributoListNewAtributo = em.merge(oldTipoAtributoOfAtributoListNewAtributo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipoAtributo.getIdTipoAtributo();
                if (findTipoAtributo(id) == null) {
                    throw new NonexistentEntityException("The tipoAtributo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoAtributo tipoAtributo;
            try {
                tipoAtributo = em.getReference(TipoAtributo.class, id);
                tipoAtributo.getIdTipoAtributo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipoAtributo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Atributo> atributoListOrphanCheck = tipoAtributo.getAtributoList();
            for (Atributo atributoListOrphanCheckAtributo : atributoListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TipoAtributo (" + tipoAtributo + ") cannot be destroyed since the Atributo " + atributoListOrphanCheckAtributo + " in its atributoList field has a non-nullable tipoAtributo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tipoAtributo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TipoAtributo> findTipoAtributoEntities() {
        return findTipoAtributoEntities(true, -1, -1);
    }

    public List<TipoAtributo> findTipoAtributoEntities(int maxResults, int firstResult) {
        return findTipoAtributoEntities(false, maxResults, firstResult);
    }

    private List<TipoAtributo> findTipoAtributoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TipoAtributo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TipoAtributo findTipoAtributo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TipoAtributo.class, id);
        } finally {
            em.close();
        }
    }

    public int getTipoAtributoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TipoAtributo> rt = cq.from(TipoAtributo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
